home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / snippet.exe / STPTOK.C < prev    next >
C/C++ Source or Header  |  1992-04-27  |  1KB  |  40 lines

  1. /*
  2. **  stptok() -- public domain by Ray Gardner, modified by Bob Stout
  3. **
  4. **   You pass this function a string to parse, a buffer to receive the
  5. **   "token" that gets scanned, the length of the buffer, and a string of
  6. **   "break" characters that stop the scan.  It will copy the string into
  7. **   the buffer up to any of the break characters, or until the buffer is
  8. **   full, and will always leave the buffer null-terminated.  It will
  9. **   return a pointer to the first non-breaking character after the one
  10. **   that stopped the scan.
  11. */
  12.  
  13. #include <string.h>
  14.  
  15. char *stptok(const char *s, char *tok, size_t toklen, char *brk)
  16. {
  17.       char *lim, *b;
  18.  
  19.       if (!*s)
  20.             return NULL;
  21.  
  22.       lim = tok + toklen - 1;
  23.       while ( *s && tok < lim )
  24.       {
  25.             for ( b = brk; *b; b++ )
  26.             {
  27.                   if ( *s == *b )
  28.                   {
  29.                         *tok = 0;
  30.                         for (++s, *s && strchr(brk, *s); ++s)
  31.                               ;
  32.                         return s;
  33.                   }
  34.             }
  35.             *tok++ = *s++;
  36.       }
  37.       *tok = 0;
  38.       return ++s;
  39. }
  40.